﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ILRuntime;

namespace Battle
{
    public static class LevelScriptMgr 
    {
        private static ILevelScript curScript;
        public static int Level { get; private set; } = 0;
        private static UpdateMono myMono;

        public static void NextLevel()
        {
            if (myMono == null)
            {
                GameObject obj = new GameObject("LevelScriptMgr.Update");
                MonoBehaviour.DontDestroyOnLoad(obj);
                myMono = obj.AddComponent<UpdateMono>();
            }
            curScript?.StopLevel();
            if (curScript == null)
                curScript = GetLevelScript();
            curScript.StartLevel(Level++);
        }
        public static void StopLevel()
        {
            curScript?.StopLevel();
            Level = 0;
        }
        private static void Update()
        {
            curScript?.OnUpdate(Time.deltaTime);
        }

        private static ILevelScript GetLevelScript()
        {
            if (ILRuntimeTools.GetHotFixSetting(ILRuntimeSettingConst.UseHotFixEnemyLevel) > 0)
            {
                string type = ILRuntimeTools.GetHotFixString(ILRuntimeSettingConst.HotFixEnemyLevelTpye);
                if (type != null)
                {
                    var obj = ILRuntimeTools.GetHotFixObj(type);
                    return obj as ILevelScript;
                }
            }

            return new LevelScript_Normal();
        }

        #region Mono
        private class UpdateMono : MonoBehaviour
        {
            private void Update()
            {
                LevelScriptMgr.Update();
            }
        }
        #endregion
    }
}
